package cc.rengu.igas.share.common.util;


import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/7/2 9:13
 */
public class DateShareUtil {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 日期转换MMdd-->yyyyMMdd
     *
     * @param settleDate 8位清算日期
     * @return 转换后成4位日期
     */
    public static String changeDate4To8(String settleDate) {
        if (settleDate.length() != 4) {
            return settleDate;
        }
        String yearSettleDate = DateUtil.getCurrentDate();
        String year = yearSettleDate.substring(0, 4);
        String month = yearSettleDate.substring(4, 6);
        String settleMonth = settleDate.substring(0, 2);
        if ("01".equals(settleMonth) && "12".equals(month)) {
            return String.format("%d", Integer.parseInt(year) + 1) + settleDate;
        } else if ("12".equals(settleMonth) && "01".equals(month)) {
            return String.format("%d", Integer.parseInt(year) - 1) + settleDate;
        }
        return year + settleDate;
    }

    /**
     * @param srcDateTime 起始时间
     * @param format      时间格式
     * @param timeFlag    MS/ms 毫秒； S/s 秒；M/m 分钟；H/h 小时；D/d 天；MH/mh 月；Y/y 年
     * @param time        单位同timeFlag
     * @return 时间
     * @throws ParseException 异常
     */
    public static String getPastOrFutureDayTime(String srcDateTime, String format, String timeFlag, int time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date srcDateTime1 = simpleDateFormat.parse(srcDateTime);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(srcDateTime1);
        switch (timeFlag) {
            case ("ms"):
            case ("MS"):
                calendar.add(Calendar.MILLISECOND, time);
            case ("S"):
            case ("s"):
                //秒
                calendar.add(Calendar.SECOND, time);
                break;
            case ("M"):
            case ("m"):
                calendar.add(Calendar.MINUTE, time);
                break;
            case ("H"):
            case ("h"):
                calendar.add(Calendar.HOUR_OF_DAY, time);
                break;
            case ("D"):
            case ("d"):
                calendar.add(Calendar.DAY_OF_MONTH, time);
                break;
            case ("MH"):
            case ("mh"):
                calendar.add(Calendar.MONTH, time);
                break;
            case ("Y"):
            case ("y"):
                calendar.add(Calendar.YEAR, time);
                break;
            default:
                calendar.add(Calendar.SECOND, time);
                break;
        }
        Date date = calendar.getTime();
        return simpleDateFormat.format(date);
    }

    public static boolean checkDateFormat(String sDate, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        Date date;
        try {
            date = simpleDateFormat.parse(sDate);
            return sDate.equals(simpleDateFormat.format(date));
        } catch (ParseException e) {
            return false;
        }
    }
}
